package com.gopay.webtest.xml;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Repository;
import org.springframework.util.ResourceUtils;

@Repository
public class XmlDao {
	private static String ROOTPATH = "/data/url/";
	private Map<String, Object> xmlMap = null;

	public Map<String, Object> getXmlMap() {
		return xmlMap;
	}

	@Resource(name = "xmlMap")
	public void setXmlMap(Map<String, Object> xmlMap) {
		this.xmlMap = xmlMap;
	}

	private Document loadXML4Doc(String xmlFile) throws DocumentException,
			IOException {
		SAXReader reader = new SAXReader();
		String fileName = null;
		Document doc = null;
		for (Map.Entry<String, Object> entry : xmlMap.entrySet()) {

			if (xmlFile.equals(entry.getKey())) {

				fileName = (String) entry.getValue();
				break;
			}
		}
		if (fileName != null) {
			File clsFile = ResourceUtils.getFile(fileName);
			doc = reader.read(clsFile);
		} else {

			throw new DocumentException("file is not available");
		}

		if (doc == null) {
			throw new DocumentException("file is not available");
		}
		return doc;
	}

	/**
	 * get the nodes from elements named ename
	 *
	 * @param xmlFile
	 * @param ename
	 * @return
	 * @throws DocumentException
	 * @throws IOException
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map<String, String> readData4Map(String xmlFile, String ename)
			throws DocumentException, IOException {

		Document doc = loadXML4Doc(xmlFile);
		Element root = doc.getRootElement();

		List nodes = root.elements(ename);

		Map<String, String> dataMap = new HashMap<String, String>();

		for (Iterator<Element> it = nodes.iterator(); it.hasNext();) {

			Element elms = (Element) it.next();

			if (elms != null) {

				for (Iterator<Element> ite = elms.elementIterator(); ite
						.hasNext();) {

					Element e = (Element) ite.next();

					dataMap.put(e.getName(), e.getText());

				}

			}

		}

		return dataMap;

	}

	/**
	 * get the element text by name
	 * 
	 * @param nodeName
	 * @return
	 * @throws DocumentException
	 * @throws IOException
	 */
	/*public String readData(ServletContext context, String xmlFile,
			String nodeName) throws DocumentException, IOException {

		Document doc = loadXML4Doc(xmlFile);
		Element root = doc.getRootElement();
		String text = root.elementText(nodeName);
		return text;
	}*/

	/**
	 * get the doc attribute value by name
	 * 
	 * @param attrName
	 * @return
	 * @throws DocumentException
	 * @throws IOException
	 */
	/*public String readAttribute(ServletContext context, String xmlFile,
			String attrName) throws DocumentException, IOException {

		Document doc = loadXML4Doc(xmlFile);
		Element root = doc.getRootElement();
		String text = root.attributeValue(attrName);

		return text;
	}*/

	// /projectDescription/projects/project"
	@SuppressWarnings("rawtypes")
	public Map<String, String> readNodesByXpath(String xmlFile, String xpath)
			throws DocumentException, IOException {

		Map<String, String> datas = new HashMap<String, String>();

		Document doc = loadXML4Doc(xmlFile);
		
		List list = doc.selectNodes(xpath);
		
		Iterator it = list.iterator();

		while (it.hasNext()) {
			Element elm = (Element) it.next();

			List ls = elm.elements();

			if (ls != null && ls.size() > 1) {

				for (Object e : ls) {

					datas.put(((Element) e).getName(), ((Element) e).getText());

				}

			} else {

				Element el = (Element) ls.get(0);
				datas.put(el.getName(), el.getName());
			}
		}

		return datas;
	}
	

	@SuppressWarnings("unchecked")
	public Map<String,String> readUris(String xmlFile, String evnNode,Map<String,String> datas) throws DocumentException, IOException{
		
//		Map<String, String> datas = new HashMap<String, String>();

		Document doc = loadXML4Doc(xmlFile);
		
		List list = doc.selectNodes(ROOTPATH + evnNode + "/uris/uri");
		
		Iterator it = list.iterator();
		
		while (it.hasNext()) {
			
			Element e = (Element) it.next(); 
			
			List<Attribute> lt =e.attributes();
			
			for(Attribute att : lt){
				
				 datas.put(att.getText(), e.getText());
				
			}
			
			
		}

		return datas;
		
	}
	
	public Map<String,String> readEvnNode(String xmlFile, String evnNode) throws DocumentException, IOException{
		
		Map<String, String> datas = new HashMap<String, String>();

		Document doc = loadXML4Doc(xmlFile);
		
		List list = doc.selectNodes(ROOTPATH + evnNode + "/dom");
		
		Iterator it = list.iterator();
		
		while (it.hasNext()) {
			
			Element e = (Element)it.next();
			
			datas.put(e.getName(), e.getText());
			
		}
		
		return readUris(xmlFile,evnNode,datas);
		
		
	}
	
	
}
